

package modelos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MDB{
    protected Connection conexion;
    protected ResultSet rs;
    protected  int codigo;
    protected  String estatus;
    
    public MDB() {
        rs=null;
    }

    public void abrir(){
        try {
            
            conexion = DriverManager.getConnection("jdbc:postgresql://localhost:5432/ComidaRapida", "postgres", "17600894");
            conexion.setAutoCommit(false);
        } catch (SQLException ex) {
            Logger.getLogger(MDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void cerrar(){
        try {
            conexion.setAutoCommit(true);
            conexion.close();
            
        } catch (SQLException ex) {
            Logger.getLogger(MDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    protected PreparedStatement crearPreparedStatement(String sql){
        PreparedStatement stmt = null;

        try {
            stmt = conexion.prepareStatement(sql);
        } catch (SQLException ex) {
            Logger.getLogger(MDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    
        return stmt;
    }    

    protected Statement crearStatement(){
        Statement stm = null;
        try {
            stm = conexion.createStatement();          
        } catch (SQLException ex) {
            Logger.getLogger(MDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return stm;
    }


 public boolean eliminar(String tabla){
     abrir();
     boolean sw= false;
     String sql="UPDATE "+tabla+" SET estatus = 'E' WHERE codigo=?  AND estatus = 'A'";
     PreparedStatement stam = crearPreparedStatement(sql);
     try {
         stam.setInt(1, getCodigo());
         stam.executeUpdate();
         sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     cerrar();
     return sw;
 }

 public boolean validarCodigo(String tabla){
     abrir();
     boolean sw= false;
     String sql="SELECT * FROM "+tabla+" WHERE codigo=?  AND estatus = 'A'";
     PreparedStatement stam = crearPreparedStatement(sql);
     try {
         stam.setInt(1, getCodigo());
         rs = stam.executeQuery();
         if(rs.next())
             sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     cerrar();
     return !sw;
 }

 public boolean validarDescripcion(String tabla, String descripcion){
     abrir();
     boolean sw= false;
     String sql="SELECT * FROM "+tabla+" WHERE codigo=?  AND descripcion =? AND estatus = 'A'";
     PreparedStatement stam = crearPreparedStatement(sql);
     try {
         stam.setInt(1, getCodigo());
         stam.setString(2, descripcion);
         rs = stam.executeQuery();
          if(rs.next())
             sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     cerrar();
     return !sw;
 }


    public int getCodigo() {
        return codigo;
    }

    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    public String getEstatus() {
        return estatus;
    }

    public void setEstatus(String estatus) {
        this.estatus = estatus;
    }

    
}
